﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BesAsm.Stormwater.HspfCore;

namespace BesAsm.Stormwater.HspfResults
{
  public class L61PerlandBlock : L61Block
  {
    public L61PerlandBlock(ILandSegment segment)
      : base(segment)
    {
    }

    public override int BlockLineCount { get { return 27; } }        

    protected override L61Fluxes ParseFluxes(List<string> blockLines)
    {
      L61Fluxes fluxes = new L61Fluxes(this.Segment.Type);
      int fluxHeaderLine = -1;

      fluxHeaderLine = blockLines.FindIndex(p => p.Contains("EXTNL INFLOWS & OUTFLOWS"));
      
      if (fluxHeaderLine == -1)
        throw new Exception("Error parsing flux block");

      string[] tokens = blockLines[fluxHeaderLine + 3].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

      fluxes.Supy = double.Parse(tokens[0]);
      fluxes.Suro = double.Parse(tokens[1]);
      fluxes.Ifwo = double.Parse(tokens[2]);
      fluxes.Agwo = double.Parse(tokens[3]);
      fluxes.Pero = double.Parse(tokens[4]);
      fluxes.Igwi = double.Parse(tokens[5]);

      return fluxes;

    }

    protected override L61StateVariables ParseStates(List<string> blockLines)
    {
      L61StateVariables states = new L61StateVariables(this.Segment.Type);
      int stateHeaderLine = -1;

      stateHeaderLine = blockLines.FindIndex(p => p.Contains("STATE VARIABLES"));    

      if (stateHeaderLine == -1)
        throw new Exception("Error parsing flux block");

      string[] tokens = blockLines[stateHeaderLine + 2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

      states.Pers = double.Parse(tokens[0]);
      states.Ceps = double.Parse(tokens[1]);
      states.Surs = double.Parse(tokens[2]);
      states.Uzs = double.Parse(tokens[3]);
      states.Ifws = double.Parse(tokens[4]);
      states.Lzs = double.Parse(tokens[5]);
      states.Agws = double.Parse(tokens[6]);
      states.Gwvs = double.Parse(tokens[7]);      

      return states;
    }

    public override Dictionary<string, object> GetResultsDictionary()
    {
      Dictionary<string, object> results = new Dictionary<string, object>();

      results.Add("Date", ResultsDate.ToString("MMMM yyyy"));
      results.Add("SUPY", Fluxes.Supy);
      results.Add("SURO", Fluxes.Suro);
      results.Add("IFWO", Fluxes.Ifwo);
      results.Add("AGWO", Fluxes.Agwo);
      results.Add("PERO", Fluxes.Pero);
      results.Add("IGWI", Fluxes.Igwi);

      return results;
    }
  }
}
